﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Models;
using DAL;

namespace 包覆门.MxExpression
{
    public partial class MxExpressionEditing : Form
    {
        public MxExpressionEditing()
        {
            InitializeComponent();
        }
        public  string EditingId { get; set; }//编辑主键
        public bool IsNew { get; set; }//是否新增、
        public bool IsCopy { get; set; }//是否复制
        private MxExpressionDAL dal = new MxExpressionDAL();

        //加载数据
        private void MxExpressionEditing_Load(object sender, EventArgs e)
        {
            if (IsNew)
            {
            }
            else 
            {
                MxTypeExpression mt = dal.GetMxTypeExpressionById(EditingId);
                this.tbmxtype.Text = mt.Mxtype;
                this.tbwlsl.Text = mt.Wlyl;
                this.tbGao.Text = mt.Gao;
                this.tbKuan.Text = mt.Kuan;
                this.tbNoZhHeight.Text = mt.nozhheight;
                this.tbNoZhWidth.Text = mt.nozhwidth;
                this.tbNoZhXbyl.Text = mt.nozhxbyl;
                this.tbremarks.Text = mt.remarks;
                if (mt.ispb == 0)
                {
                    SetControlState(true);
                    this.rbYes.Checked = true;
                    this.tbDefaultWidth.Text = mt.defaultwidth;
                    this.tbDefaultTopHeight.Text = mt.defaulttopheight;
                    this.tbDefaultDownHeight.Text = mt.defaultdownheight;
                    this.tbDefaultXbyl.Text = mt.defaultxbyl;
                    this.tbDefineWidth.Text = mt.definewidth;
                    this.tbDefineTopHeight.Text = mt.definetopheight;
                    this.tbDefineDownHeight.Text = mt.definedownheight;
                    this.tbDefineXbyl.Text = mt.definexbyl;
                }
                else {
                    this.rbNo.Checked = true;
                    SetControlState(false);
                }
             }
        }

        //保存
        private void btnSave_Click(object sender, EventArgs e)
        {
            MxTypeExpression mt = new MxTypeExpression();
            mt.Mxtype = this.tbmxtype.Text.Trim();
            foreach (RadioButton rb in this.gbpb.Controls) {
                if (rb.Checked) {
                    mt.ispb = Convert.ToInt32(rb.Tag);
                }
            }
            mt.Kuan = this.tbKuan.Text.Trim();
            mt.Gao = this.tbGao.Text.Trim();
            mt.nozhwidth = this.tbNoZhWidth.Text.Trim().ToLower();
            mt.nozhheight = this.tbNoZhHeight.Text.Trim().ToLower();
            mt.nozhxbyl = this.tbNoZhXbyl.Text.Trim().ToLower();
            mt.Wlyl = this.tbwlsl.Text.Trim().ToLower();
            mt.remarks = this.tbremarks.Text.Trim();
            if (mt.ispb == 0) {
                mt.defaultwidth = this.tbDefaultWidth.Text.Trim().ToLower();
                mt.defaulttopheight = this.tbDefaultTopHeight.Text.Trim().ToLower(); 
                mt.defaultdownheight = this.tbDefaultDownHeight.Text.Trim().ToLower();
                mt.defaultxbyl = this.tbDefaultXbyl.Text.Trim().ToLower();

                mt.definewidth = this.tbDefineWidth.Text.Trim().ToLower();
                mt.definetopheight = this.tbDefineTopHeight.Text.Trim().ToLower();
                mt.definedownheight = this.tbDefineDownHeight.Text.Trim().ToLower();
                mt.definexbyl = this.tbDefineXbyl.Text.Trim().ToLower();
            }
            if (ValidateData(mt))
            {
                if (IsNew||IsCopy)
                {
                    int result = dal.SaveData(mt);
                    if (result == 1)
                    {
                        MessageBox.Show("保存成功");
                        DialogResult = DialogResult.OK;
                    }
                    else
                    {
                        MessageBox.Show("保存失败");
                        DialogResult = DialogResult.No;
                    }
                }
                else
                {
                    mt.Mid = Convert.ToInt32(EditingId);
                    int result = dal.UpdateData(mt);
                    if (result == 1)
                    {
                        MessageBox.Show("保存成功");
                        DialogResult = DialogResult.OK;
                    }
                    else
                    {
                        MessageBox.Show("保存失败");
                        DialogResult = DialogResult.No;
                    }

                }
            }
        }

        //关闭
        private void btnCancel_Click(object sender, EventArgs e)
        {
            Close();
        }
        //状态机
        public void SetControlState(bool flag) {
            this.tbDefaultWidth.Enabled = flag;
            this.tbDefaultTopHeight.Enabled = flag;
            this.tbDefaultDownHeight.Enabled = flag;
            this.tbDefaultXbyl.Enabled = flag;
            this.tbDefineWidth.Enabled = flag;
            this.tbDefineTopHeight.Enabled = flag;
            this.tbDefineDownHeight.Enabled = flag;
            this.tbDefineXbyl.Enabled = flag;
        }
        //判断单选按钮 确定状态机
        private void rbYes_CheckedChanged(object sender, EventArgs e)
        {
            if (this.rbYes.Checked)
            {
                SetControlState(true);
            }
            else {
                SetControlState(false);
            }
        }
        //校验公式
        private void btnTest_Click(object sender, EventArgs e)
        {
            try
            {
                MSScriptControl.ScriptControl sc = new MSScriptControl.ScriptControl();
                sc.Language = "JavaScript";
                string s = this.tbTest.Text.Trim().ToLower();
                if (string.IsNullOrWhiteSpace(s))
                {
                    MessageBox.Show("请填写配置公式");
                }
                else 
                {
                    s = s.Replace("gao", "2000").Replace("kuan", "2000").Replace("xbheight", "2000").Replace("xbwidth", "2000").Replace("xbtopheight", "2000").Replace("xbdownheight", "2000").Replace("num", "2");
                    sc.Eval(s);
                    MessageBox.Show("测试成功");
                }
            }
            catch (Exception ex)
            {

                MessageBox.Show("公式配置错误，错误内容："+ex.Message);
            }
        }
        //校验必填字段
        public bool ValidateData(MxTypeExpression mt) {
            bool flag = true;
            if (string.IsNullOrWhiteSpace(mt.Mxtype))
            {
                MessageBox.Show("门型不能为空！");
                flag = false;
                return flag;
            }
            else {
                if (IsNew || IsCopy)
                {
                    DataTable dt = dal.GetMxExpressionByMxType(mt.Mxtype);
                    if (dt.Rows.Count > 0)
                    {
                        MessageBox.Show("已经存在门型" + mt.Mxtype + "公式,请不要重复添加！");
                        flag = false;
                        return flag;
                    }
                }
                
            }
            if (string.IsNullOrWhiteSpace(mt.Kuan))
            {
                MessageBox.Show("宽不能为空！");
                flag = false;
                return flag;
            }
            if (string.IsNullOrWhiteSpace(mt.Mxtype))
            {
                MessageBox.Show("高不能为空！");
                flag = false;
                return flag;
            }
            if (string.IsNullOrWhiteSpace(mt.Wlyl))
            {
                MessageBox.Show("框型用量公式不能为空！");
                flag = false;
                return flag;
            }
            if (string.IsNullOrWhiteSpace(mt.nozhwidth))
            {
                MessageBox.Show("请输入无中横宽公式");
                flag = false;
                return flag;
            }
            if (string.IsNullOrWhiteSpace(mt.nozhheight))
            {
                MessageBox.Show("请输入无中横高公式");
                flag = false;
                return flag;
            }
            if (string.IsNullOrWhiteSpace(mt.nozhxbyl))
            {
                MessageBox.Show("请输入无中横芯板用量公式");
                flag = false;
                return flag;
            }
            if (mt.ispb==0)
            {
                if (string.IsNullOrWhiteSpace(mt.defaultwidth))
                {
                    MessageBox.Show("请输入默认（下四上六）芯板宽公式");
                    flag = false;
                    return flag;
                }
                if (string.IsNullOrWhiteSpace(mt.defaulttopheight))
                {
                    MessageBox.Show("请输入默认（下四上六）上芯板高公式");
                    flag = false;
                    return flag;
                }
                if (string.IsNullOrWhiteSpace(mt.defaultdownheight))
                {
                    MessageBox.Show("请输入默认（下四上六）下芯板高公式");
                    flag = false;
                    return flag;
                }
                if (string.IsNullOrWhiteSpace(mt.defaultxbyl))
                {
                    MessageBox.Show("请输入默认（下四上六）下芯板用量公式");
                    flag = false;
                    return flag;

                }
                if (string.IsNullOrWhiteSpace(mt.definewidth))
                {
                    MessageBox.Show("请输入自定义芯板宽公式");
                    flag = false;
                    return flag;
                }
                if (string.IsNullOrWhiteSpace(mt.definetopheight))
                {
                    MessageBox.Show("请输入自定义上芯板高公式");
                    flag = false;
                    return flag;
                }
                if (string.IsNullOrWhiteSpace(mt.definewidth))
                {
                    MessageBox.Show("请输入自定义下芯板高公式");
                    flag = false;
                    return flag;
                }
                if (string.IsNullOrWhiteSpace(mt.definewidth))
                {
                    MessageBox.Show("请输入自定义芯板用量公式");
                    flag = false;
                    return flag;
                }

            }
            return flag;
        }
        //限制输入数字
        private void tbKuan_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == '.' && this.tbKuan.Text.IndexOf(".") != -1)
            {
                e.Handled = true;
            }

            if (!((e.KeyChar >= 48 && e.KeyChar <= 57) || e.KeyChar == '.' || e.KeyChar == 8))
            {
                e.Handled = true;
            }  
        }
        //限制数字
        private void tbGao_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == '.' && this.tbGao.Text.IndexOf(".") != -1)
            {
                e.Handled = true;
            }

            if (!((e.KeyChar >= 48 && e.KeyChar <= 57) || e.KeyChar == '.' || e.KeyChar == 8))
            {
                e.Handled = true;
            }  
        }
    }
}
